using UnityEngine;
using System.Collections;

/// \class  gkInputAccelerometer
/// \brief  The accelerometer input wrapper
[AddComponentMenu("GK/Engine/Core/Input/gkInputAccelerometer")]
public class gkInputAccelerometer : MonoBehaviour
{
    /// The maximum tilt rotation in degree
    public float MaxTiltInDegree = 20.0f;

    /// The current tilt in percent of the total allowed amplitude
    private Vector3 m_f3TiltPercent = new Vector3(0, 0, 0);
	
    /// \brief  Get the tilt percent
    public Vector3 GetTiltPercent()
    {
        return m_f3TiltPercent;
    }

    /// \brief  Called each frame
    void Update()
    {	
		if(Application.platform == RuntimePlatform.IPhonePlayer)
		{
			Vector3 f3DeviceAttitudeRemap = new Vector3(0,0,0);
			
	        // If there is an accelerometer consider the attitude
	        f3DeviceAttitudeRemap.x = Input.acceleration.y;
	        f3DeviceAttitudeRemap.y = -Input.acceleration.x;
	        f3DeviceAttitudeRemap.z = 0.0f;
			
	        // Scale the tilt in the range [-m_fMaxTiltInDegree, m_fMaxTiltInDegree]
	        m_f3TiltPercent = (f3DeviceAttitudeRemap*Mathf.Rad2Deg)/MaxTiltInDegree;
		}
		else
		{
	        // Else take a stick
	        m_f3TiltPercent.x = Input.GetAxis("Horizontal");
	        m_f3TiltPercent.y = Input.GetAxis("Vertical");
	
	        // Clamp
	        // X
	        if(m_f3TiltPercent.x > 1.0f)
	        {
		        m_f3TiltPercent.x = 1.0f;
	        }
	        else if(m_f3TiltPercent.x < -1.0f)
	        {
		        m_f3TiltPercent.x = -1.0f;
	        }
	
	        // Y
	        if(m_f3TiltPercent.y > 1.0f)
	        {
		        m_f3TiltPercent.y = 1.0f;
	        }
	        else if(m_f3TiltPercent.y < -1.0f)
	        {
		        m_f3TiltPercent.y = -1.0f;
	        }	
		}	
    }
}
